package com.fr.data.impl;

import com.fr.base.FRContext;
import com.fr.base.StringUtils;
import com.fr.base.core.ComparatorUtils;
import com.fr.base.core.list.FRCacheList;
import com.fr.data.TableDataException;
import com.fr.data.core.DataUtils;
import com.fr.data.core.db.ColumnInformation;
import com.fr.data.core.db.dialect.Dialect;
import com.fr.data.core.db.dialect.DialectFactory;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

public final class GroupCachedAdapter extends TableDataAdapter
{
  private static final long serialVersionUID = 1L;
  private transient List row_list;
  private transient int current_group_start_index = 0;
  private transient int next_group_start_index = 0;
  private int key_column_index = -2;
  private String key_column_name;
  private transient Object last_key_value;

  public GroupCachedAdapter(DatabaseConnection paramDatabaseConnection, String paramString1, String paramString2)
  {
    super(paramDatabaseConnection, paramString1, 0);
    this.key_column_name = paramString2;
  }

  public int getRowCount()
    throws TableDataException
  {
    int i = shortcutGetRowCount();
    if (i < 0)
    {
      hasRow(2147483647);
      return (this.current_group_start_index + this.row_list.size());
    }
    return i;
  }

  public Object getValueAt(int paramInt1, int paramInt2)
    throws TableDataException
  {
    return ((hasRow(paramInt1)) ? ((Object[])this.row_list.get(paramInt1 - this.current_group_start_index))[paramInt2] : null);
  }

  protected boolean hasRow(int paramInt)
    throws TableDataException
  {
    if (paramInt < 0)
      return false;
    if (paramInt < this.current_group_start_index)
    {
      try
      {
        releaseConnection();
      }
      catch (Exception localException1)
      {
        throw new TableDataException(localException1.getMessage(), localException1);
      }
      this.row_list.clear();
      this.last_key_value = new TableDataException("我是个异常，你敢和我一样？");
      this.current_group_start_index = 0;
      this.next_group_start_index = 0;
      hasRow(paramInt);
    }
    else if ((this.row_list != null) && (this.row_list.size() > paramInt - this.current_group_start_index))
    {
      return true;
    }
    if (this.isCheckRowOver)
      return false;
    initConnectionAndResultAndCheckInColumns();
    if (this.connection == null)
      return false;
    Dialect localDialect = DialectFactory.generateDialect(this.connection);
    int i = getColumnCount();
    String str1 = this.database.getOriginalCharsetName();
    String str2 = this.database.getNewCharsetName();
    try
    {
      do
      {
        if (!(this.resultSet.next()))
          break label453;
        Object[] arrayOfObject = new Object[i];
        for (int j = 0; j < i; ++j)
          try
          {
            int k;
            arrayOfObject[j] = DataUtils.getResultSetObject(this.resultSet, this.columnInformations[j].getColumnType(), j + 1, str1, str2, localDialect);
            if (this.key_column_index == -2)
            {
              this.key_column_index = -1;
              if (StringUtils.isNotBlank(this.key_column_name))
                for (k = 0; k < getColumnCount(); ++k)
                  if ((getColumnName(k) != null) && (this.key_column_name.equalsIgnoreCase(getColumnName(k))))
                    this.key_column_index = k;
            }
            if (j == this.key_column_index)
            {
              if (this.current_group_start_index < this.next_group_start_index)
              {
                Object localObject = this.row_list.get(this.row_list.size() - 1);
                this.row_list.clear();
                this.row_list.add(localObject);
                this.current_group_start_index = this.next_group_start_index;
              }
              if (!(ComparatorUtils.equals(arrayOfObject[j], this.last_key_value)))
              {
                this.next_group_start_index += this.row_list.size();
                this.last_key_value = arrayOfObject[j];
              }
            }
          }
          catch (SQLException localSQLException)
          {
            arrayOfObject[j] = null;
          }
        this.row_list.add(arrayOfObject);
      }
      while (this.row_list.size() + this.current_group_start_index <= paramInt);
      return true;
      label453: this.isCheckRowOver = true;
      releaseConnection();
    }
    catch (Exception localException2)
    {
      try
      {
        releaseConnection();
      }
      catch (Exception localException3)
      {
        FRContext.getLogger().log(Level.WARNING, "Error happens while releaseConnection");
      }
      this.isCheckRowOver = true;
      throw new TableDataException(localException2.getMessage() + "\nQuery is:" + this.sql + "\nrowIndex to get is:" + paramInt, localException2);
    }
    return (this.row_list.size() + this.current_group_start_index > paramInt);
  }

  protected void initRowValueStatus()
  {
    this.row_list = new FRCacheList(5000, "GCA");
    this.last_key_value = new TableDataException("我是个异常，你敢和我一样？");
    this.current_group_start_index = 0;
    this.next_group_start_index = 0;
  }

  protected void release()
    throws SQLException
  {
    if (this.row_list != null)
      this.row_list.clear();
    super.release();
  }
}